#!/usr/bin/env python
#coding:utf-8
import os
import urllib2
#from urllister import URLLister
from sgmllib import SGMLParser

class URLLister(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.url = []
        self.img = []

    def start_a(self, attrs):
        href = [v for k, v in attrs if k=='href']
        if href:
            self.url.extend(href)

    def start_img(self, attrs):
        src = [v for k, v in attrs if k=='src']
        if src:
            self.img.extend(src)


website = 'http://webpy.org/cookbook/index.zh-cn'

def readIndex(url):
    sock = urllib2.urlopen(url)
    html = sock.read()
    saveHtml(html)

def saveHtml(html, filename='index.html'):
    print 'Save as:' + filename
    if not os.path.isdir('cookbook/'):
        os.mkdir('cookbook')
    f = file(r'cookbook/' + filename, 'w')
    f.write(html)
    f.close()

def page(website):
    count = 0
    names = []
    sock = urllib2.urlopen(website)
    html = sock.read()
    #print html
    p = URLLister()
    p.feed(html)
    for url in p.url:
        
        if (len(url.split('/')) == 3) and (url[-5:] == 'zh-cn'):
            path = url.split('/')[1]
            #print path
            names.append(path)
            
    for name in names:
        #print "Save as: %s" % name
        myurl = 'http://webpy.org/cookbook/'+\
                name + '.zh-cn'
        #print myurl
        try:
            myhtml = urllib2.urlopen(myurl).read()
            saveHtml(myhtml, '%s_%s.html' % (count, name))
        except IOError:
            print 'Oops!"%s" not found!' % myurl
            pass
        count += 1

def main():
    readIndex(website)
    page(website)

if __name__ == '__main__':
    main()
    
